package fic.storefinder.client.content;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;

import fic.storefinder.client.listener.UpdateDBListener;
import fic.storefinder.shared.serializable.POI;
import fic.storefinder.shared.serializable.Robe;
import fic.storefinder.shared.utils.Constants;
import com.google.gwt.user.client.ui.SimpleCheckBox;

public class EditRobeDialog extends DialogBox {
	
	
	private UpdateDBListener updateListener;
	//private POI poi;
	private TextBox descTextBox;
	private TextBox priceTextBox;
	private TextBox currencyTextBox;
	
	
	private Label lblDesc;
	private Label lblPrice;
	private Label lblCurrency;
	private Label lblAvailable;
	private SimpleCheckBox availableCheckBox;
	
	
	public EditRobeDialog(final int robeEditType, final Robe robe, UpdateDBListener updateListener) {
		this.updateListener = updateListener;
		//this.poi = poi;
		VerticalPanel verticalPanel = new VerticalPanel();
		setWidget(verticalPanel);
		verticalPanel.setSize("100%", "100%");
		
		FlexTable flexTable = new FlexTable();
		verticalPanel.add(flexTable);
		
		lblDesc = new Label("Leírás:");
		lblDesc.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(0, 0, lblDesc);
		
		descTextBox = new TextBox();
		descTextBox.setAlignment(TextAlignment.LEFT);
		flexTable.setWidget(0, 1, descTextBox);
		
		lblPrice = new Label("Ar:");
		lblPrice.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(1, 0, lblPrice);
		
		priceTextBox = new TextBox();
		flexTable.setWidget(1, 1, priceTextBox);
		
		lblCurrency = new Label("Pénznem:");
		lblCurrency.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(2, 0, lblCurrency);
		
		currencyTextBox = new TextBox();
		flexTable.setWidget(2, 1, currencyTextBox);
		
		lblAvailable = new Label("Elérhető?");
		lblAvailable.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		flexTable.setWidget(3, 0, lblAvailable);
		
		availableCheckBox = new SimpleCheckBox();
		flexTable.setWidget(3, 1, availableCheckBox);
		
		
		
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		verticalPanel.add(horizontalPanel);
		
		if (robeEditType == Constants.EDIT_TYPE_UPDATE) {
			descTextBox.setText(robe.getDescription());
			priceTextBox.setText(robe.getPrice() + "");
			currencyTextBox.setText(robe.getCurrency());
			availableCheckBox.setValue(robe.getAvailable() == Constants.ROBE_AVAILABLE_YES ? true : false);
		}
		
		Button btnSave = new Button("Mentés");
		horizontalPanel.add(btnSave);
		btnSave.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (checkInputFields()) {
					Robe robeToModify = new Robe();
					robeToModify.setDescription(descTextBox.getText());
					robeToModify.setPrice(Integer.parseInt(priceTextBox.getText()));
					robeToModify.setCurrency(currencyTextBox.getText());
					robeToModify.setAvailable(availableCheckBox.getValue() ? Constants.ROBE_AVAILABLE_YES : Constants.ROBE_AVAILABLE_NO);
					robeToModify.setPoiId(robe.getPoiId());
					
					if (robe != null) {
						robeToModify.setRobeId(robe.getRobeId());
					}
					
					if (robeEditType == Constants.EDIT_TYPE_NEW) {
						EditRobeDialog.this.save(robeToModify);
					} else if (robeEditType == Constants.EDIT_TYPE_UPDATE) {
						EditRobeDialog.this.update(robeToModify);
					}
					
					EditRobeDialog.this.hide();
				}

			}
		});
		
		Button btnCancel = new Button("Mégse");
		horizontalPanel.add(btnCancel);
		btnCancel.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				EditRobeDialog.this.hide();
			}
		});
		
		if (robeEditType == Constants.EDIT_TYPE_UPDATE) {
			Button btnDelete = new Button("Törlés");
			horizontalPanel.add(btnDelete);
			btnDelete.addClickHandler(new ClickHandler() {
				
				@Override
				public void onClick(ClickEvent event) {
					EditRobeDialog.this.delete(robe);
					EditRobeDialog.this.hide();
				}
			});
		}
	} 
	
	private void save(Robe robeToSave) {
		this.updateListener.insert(robeToSave);
	}
	
	private void update(Robe robeToUpdate) {
		this.updateListener.update(robeToUpdate);
	}
	
	private void delete(Robe robeToDelete) {
		this.updateListener.delete(robeToDelete);
	}
	
	private boolean checkInputFields() {
		boolean retValue = true;
		if ("".equals(descTextBox.getText())) {
			lblDesc.setStyleName("errorTxt");
			retValue = false;
		} else {
			lblDesc.setStyleName("normalTxt");
		}
		
		if ("".equals(currencyTextBox.getText())) {
			lblCurrency.setStyleName("errorTxt");
			retValue = false;
		} else {
			lblCurrency.setStyleName("normalTxt");
		}
		
		
		
		try {
			Integer.parseInt(priceTextBox.getText());
			lblPrice.setStyleName("normalTxt");
		} catch (Exception e) {
			lblPrice.setStyleName("errorTxt");
			retValue = false;
		}
		
		return retValue;
	}
}
